package or.exa.cangguan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.common.Redis;
import or.exa.cangguan.entity.dto.LoadingPositionDto;
import or.exa.cangguan.entity.po.LoadingPosition;
import or.exa.cangguan.entity.vo.LoadingPositionVo;
import or.exa.cangguan.mapper.LoadingPositionMapper;
import or.exa.cangguan.mapper.StoreMapper;
import or.exa.cangguan.service.LoadingPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class LoadingPositionServiceImpl implements LoadingPositionService {

    @Autowired
    private LoadingPositionMapper loadingPositionMapper;

    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private Redis redis;


    //新增装卸点
    @Override
    public int addLoadingPosition(LoadingPositionVo loadingPositionVo) {
        Date date = new Date(System.currentTimeMillis());
        LoadingPosition loadingPosition = new LoadingPosition();
        Integer storeid = storeMapper.selectByName(loadingPositionVo.getStoreName());
        BeanUtil.copyProperties(loadingPositionVo, loadingPosition);
        loadingPosition.setStoreId(storeid);
        loadingPosition.setState("0");
        loadingPosition.setCreateTime(date);
        loadingPosition.setUpdateTime(date);
        loadingPosition.setCreateBy(0);
        loadingPosition.setUpdateBy(0);
        return loadingPositionMapper.insertSelective(loadingPosition);
    }


    //修改装卸点
    @Override
    public int updateLoadingPosition(LoadingPositionVo loadingPositionVo) {
        Date date = new Date(System.currentTimeMillis());
        LoadingPosition loadingPosition = new LoadingPosition();
        BeanUtil.copyProperties(loadingPositionVo, loadingPosition);
        loadingPosition.setUpdateTime(date);
        loadingPosition.setUpdateBy(0);
        log.info("----------------------------"+loadingPositionVo);
        log.info("----------------------------"+loadingPosition);
        return loadingPositionMapper.updateByPrimaryKeySelective(loadingPosition);
    }


    //删除装卸点
    @Override
    public int deleteLoadingPosition(Integer id) {
        return loadingPositionMapper.deleteByPrimaryKey(id);
    }


    //查询装卸点
    @Override
    public Page queryLoadingPosition(LoadingPositionVo loadingPositionVo) {
        Page page = new Page();
        if (loadingPositionVo.getPageNum() != null) {
            page.setPageNum(loadingPositionVo.getPageNum().intValue());
        }
        if (loadingPositionVo.getPageSize() != null) {
            page.setPageSize(loadingPositionVo.getPageSize().intValue());
        }
        
        int total = loadingPositionMapper.selectLoadingPositionCount(loadingPositionVo);
        page.setTotalNum(total);
        
        if (total > 0) {
            List<LoadingPositionDto> list = loadingPositionMapper.selectLoadingPosition(loadingPositionVo, page);
            page.setResultList(list);
        }
        
        return page;
    }


    //查询所有装卸点名
    @Override
    public List<String> selectName() {
        if (redis.idExists("loadingPositionName")) {
            List<String> loadingPositionName = (List<String>) redis.get("loadingPositionName");
            return loadingPositionName;
        } else {
            List<String> loadingPositionName = loadingPositionMapper.sealectName();
            redis.set("loadingPositionName", loadingPositionName);
            return loadingPositionName;
        }
    }
}
